package ca.llsutherland.squash.facade;


import ca.llsutherland.squash.dao.AdministratorsDao;
import ca.llsutherland.squash.dao.CourtsDao;
import ca.llsutherland.squash.dao.GreetingsDao;
import ca.llsutherland.squash.dao.GymsDao;
import ca.llsutherland.squash.dao.NewsItemsDao;
import ca.llsutherland.squash.dao.RacquetsDao;
import ca.llsutherland.squash.dao.ShoesDao;
import ca.llsutherland.squash.domain.Administrator;
import ca.llsutherland.squash.domain.Court;
import ca.llsutherland.squash.domain.Greeting;
import ca.llsutherland.squash.domain.Gym;
import ca.llsutherland.squash.domain.NewsItem;
import ca.llsutherland.squash.domain.Racquet;
import ca.llsutherland.squash.domain.Shoe;
import ca.llsutherland.squash.domain.helper.AdministratorsHelper;
import ca.llsutherland.squash.domain.helper.CourtsHelper;
import ca.llsutherland.squash.domain.helper.GreetingsHelper;
import ca.llsutherland.squash.domain.helper.GymsHelper;
import ca.llsutherland.squash.domain.helper.NewsItemsHelper;
import ca.llsutherland.squash.domain.helper.RacquetsHelper;
import ca.llsutherland.squash.domain.helper.ShoesHelper;
import ca.llsutherland.squash.exceptions.ValidationException;
import ca.llsutherland.squash.persistence.ReadOnlyTransactionHandler;
import ca.llsutherland.squash.persistence.Transaction;
import ca.llsutherland.squash.persistence.TransactionHandler;

public class AdministrationFacade {

    protected TransactionHandler transactionHandler = TransactionHandler.getInstance();
    protected ReadOnlyTransactionHandler readOnlyTransactionHandler = ReadOnlyTransactionHandler.getInstance();
    
    
    public Gym createGym(final Gym gym) throws ValidationException {
        GymsHelper.assertNewValid(gym);
        
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return GymsDao.getInstance().create(gym);
            }
        };
        return (Gym) this.transactionHandler.runTransaction(t);
    }


    public Gym updateGym(final Gym gym) {
        GymsHelper.assertValid(gym);
        
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return GymsDao.getInstance().update(gym);
            }
        };
        return (Gym) this.transactionHandler.runTransaction(t);
    }


    public void deleteGym(final Gym gym) {
        GymsHelper.assertValid(gym);
        
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                GymsDao.getInstance().delete(gym);
                return null;
            }
        };
        this.transactionHandler.runTransaction(t);
    }


    public Court createCourt(final Court court) {
        CourtsHelper.assertNewValid(court);
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return CourtsDao.getInstance().create(court);
            }
        };
        return (Court)this.transactionHandler.runTransaction(t);
    }


    public Administrator createAdministrator(final Administrator administrator) {
        AdministratorsHelper.assertNewValid(administrator);
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return AdministratorsDao.getInstance().create(administrator);
            }
        };
        return (Administrator)this.transactionHandler.runTransaction(t);
    }


    public Administrator login(final String firstname, final String lastname, final String password) {
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return AdministratorsDao.getInstance().findByFirstnameLastnamePassword(firstname, lastname, password);
            }
        };
        return (Administrator)this.readOnlyTransactionHandler.runTransaction(t);
        
    }


    public NewsItem createNews(final NewsItem newsItem) {
        NewsItemsHelper.assertNewValid(newsItem);
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return NewsItemsDao.getInstance().create(newsItem);
            }
        };
        return (NewsItem)this.transactionHandler.runTransaction(t);
    }


    public Greeting createGreetings(final Greeting greeting) {
        GreetingsHelper.assertNewValid(greeting);
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return GreetingsDao.getInstance().create(greeting);
            }
        };
        return (Greeting)this.transactionHandler.runTransaction(t);
    }
    
    public Racquet createRacquet(final Racquet racquet) {
        RacquetsHelper.assertNewValid(racquet);
        
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return RacquetsDao.getInstance().create(racquet);
            }
        };
        return (Racquet) this.transactionHandler.runTransaction(t);
    }
    
    public Shoe createShoe(final Shoe shoe) {
        ShoesHelper.assertNewValid(shoe);
        
        Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
                return ShoesDao.getInstance().create(shoe);
            }
        };
        return (Shoe) this.transactionHandler.runTransaction(t);
    }



}
